package org.lucky.admin.util;

import cn.hutool.json.JSONUtil;
import org.apache.commons.lang3.StringUtils;
import org.lucky_common.dto.AdminDto;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Duration;

//import javax.annotation.Resource;

/**
 * @author RTX 9090
 */
@Component
public class RedisUtil {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public void setId(String key, String id, int seconds) {
//        redisTemplate.setValueSerializer(new StringRedisSerializer());
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        valueOperations.set(key, id,Duration.ofSeconds(seconds));
    }

    public void set(String key, AdminDto value, int seconds) {
//        redisTemplate.setValueSerializer(new StringRedisSerializer());
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, JSONUtil.toJsonStr(value),Duration.ofSeconds(seconds));
    }

    public void setex(String key, AdminDto value, int seconds) {
//        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
//        valueOperations.set(key, JSONUtil.toJsonStr(value), 7200, TimeUnit.SECONDS);
        valueOperations.set(key, JSONUtil.toJsonStr(value), Duration.ofSeconds(seconds));
    }

    public AdminDto getKey(String token) {
        if (StringUtils.isBlank(token)) {
            return null;
        }
        //判断是否存在
        boolean exists = exists(token);
        if (!exists) {
            return null;
        }
        //判断是否过期
        Long expire = expire(token);
        if (expire == -2){
            return null;
        }
        //取
        String forValue = redisTemplate.opsForValue().get(token);
        System.out.println(forValue);
        //将json转化为对象
        AdminDto employee = JSONUtil.toBean(forValue, AdminDto.class);
        System.out.println("redis里的employee = " + employee);
        return employee;
    }

    public void logout(String token) {
        //取
        String forValue = redisTemplate.opsForValue().get(token);
        //将json转化为对象
        AdminDto employee = JSONUtil.toBean(forValue, AdminDto.class);
        synchronized (RedisUtil.class) {
//            employee.getUser().setIsLogin(0);
//            employeeService.setLogin(employee.getUser());
            if (employee.getEmployee() != null) {
//                redisTemplate.delete(employee.getEmployee().getPhone());
                redisTemplate.delete(token);
            }else {
                redisTemplate.delete(employee.getUser().getUsername());
                redisTemplate.delete(token);
            }
        }
    }

    public Long expire(String token) {
        /*
          从redis中获取key对应的过期时间;
          如果该值有过期时间，就返回相应的过期时间;
          如果该值没有设置过期时间，就返回-1;
          如果没有该值，就返回-2;
         */

        return redisTemplate.opsForValue().getOperations().getExpire(token);
    }


    /**
     * 判断key是否存在
     * @param key
     * @return
     */
    public Boolean exists(String key) {
        return stringRedisTemplate.hasKey(key);
    }

    /**
     * 判断key是否过期
     * @param key
     * @return
     */
    public boolean isExpire(String key) {
        return expire(key) <= 1;
    }

    public Boolean getKeyByName(String name) {
        return redisTemplate.hasKey(name);
    }

}